# -*- coding: utf-8 -*-c
import scrapy
import MySQLdb
import datetime
import time
from scrapy.http import Request
from urllib import parse

from horsveision.items import CsrcItem


class StcnSpider(scrapy.Spider):
    name = 'stcn'
    allowed_domains = ['news.stcn.com']
    start_urls = ['http://news.stcn.com/']
    db = MySQLdb.connect('localhost', 'root', '698a80e4212ba38f', 'toujiao', charset='utf8', use_unicode=True)
    # db = MySQLdb.connect('192.168.100.101', 'root', 'root', 'haohsi', charset='utf8', use_unicode=True)

    def parse(self, response):
        now_date = datetime.datetime.now().strftime('%Y-%m-%d')
        now_time = time.mktime(time.strptime(now_date, '%Y-%m-%d'))
        content_lists = response.css("#idData>li")
        for content_list in content_lists:
            new_title = content_list.css(".tit a::attr(title)").extract()[0]
            new_send_date = content_list.css(".sj::text").extract()[0]
            new_send_time = time.mktime(time.strptime(new_send_date, '%Y-%m-%d'))
            if int(new_send_time) == int(now_time):
                with self.db.cursor() as cursor:
                    sql = "SELECT title FROM spider_content WHERE title='%s'" % new_title
                    cursor.execute(sql)
                    result = cursor.fetchone()
                    if result is None:
                        pare_url = content_list.css(".tit a::attr(href)").extract()[0]
                        yield Request(url=parse.urljoin(response.url, pare_url), callback=self.parse_detail,
                                      dont_filter=True)

    def parse_detail(self, response):
        new_title = response.css(".box_left .intal_tit h2::text").extract()[0]
        new_send_date = response.css(".box_left .intal_tit .info::text").extract()[0]
        new_source = response.css(".box_left .intal_tit .info span::text").extract()[0].replace("来源：", "")
        new_content = response.css("#ctrlfscont p::text").extract()[0]

        stcn_item = CsrcItem()

        stcn_item['title'] = new_title
        stcn_item['send_time'] = int(time.mktime(time.strptime(new_send_date, '%Y-%m-%d %H:%M')))
        stcn_item['source'] = new_source
        stcn_item['content'] = new_content
        stcn_item['cover_img'] = ""

        yield stcn_item
